//
//  DetailEnlargeViewController.swift
//  Chihiro
//
//  Created by point on 2016/12/28.
//  Copyright © 2016年 chihiro. All rights reserved.
//

import UIKit


class DetailEnlargeViewController: UIViewController,UIGestureRecognizerDelegate,UIScrollViewDelegate {
    
    lazy var enlargeImage = UIImageView()
    fileprivate lazy var scroolView = UIScrollView()
    fileprivate let maxScale: CGFloat = 10.0 // 最大的缩放比例
    fileprivate let minScale: CGFloat = 1.0 // 最小的缩放比例
    fileprivate var isScale: Bool = false
    var myframe:CGRect = CGRect()
    var imgStr: String!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.black.withAlphaComponent(0.2)
        
        scroolView.delegate = self
        scroolView.showsVerticalScrollIndicator = false
        scroolView.showsHorizontalScrollIndicator = false
        scroolView.maximumZoomScale = maxScale // scrollView最大缩放比例
        scroolView.minimumZoomScale = minScale // scrollView最小缩放比例
        scroolView.bouncesZoom = true
        scroolView.frame = CGRect(x: 0, y: 0, width: kScreenW, height: kScreenH)
        scroolView.backgroundColor =  UIColor.black.withAlphaComponent(0.2)
        view.insertSubview(self.scroolView, at: 0)
        scroolView.addSubview(enlargeImage)
        enlargeImage.frame = myframe
        
        
        
        
        let url = URL(string: imgStr)
        enlargeImage.kf.setImage(with: url, placeholder: UIImage(named: ""))
        
        
        
        enlargeImage.isUserInteractionEnabled = true
        let pinGesutre = UITapGestureRecognizer(target: self, action: #selector(pinFunc(_:)))
        pinGesutre.numberOfTapsRequired = 1
        pinGesutre.numberOfTouchesRequired = 1
        enlargeImage.addGestureRecognizer(pinGesutre)
        
        scroolView.addGestureRecognizer(pinGesutre)
        
        
        
        let pinDoubleGesutre = UITapGestureRecognizer(target: self, action: #selector(pinFuncDouble(_:)))
        pinDoubleGesutre.numberOfTapsRequired = 2
        pinDoubleGesutre.numberOfTouchesRequired = 1
        pinGesutre.require(toFail: pinDoubleGesutre)
        enlargeImage.addGestureRecognizer(pinDoubleGesutre)
    }
    
    
    
    @objc func pinFuncDouble(_ recognizer:UITapGestureRecognizer) {
        if !isScale {
            isScale = true
            scroolView.setZoomScale(2, animated: true)
            
        } else {
            isScale = false
            scroolView.setZoomScale(0.5, animated: true)
        }
    }
    
    @objc func pinFunc(_ recognizer:UITapGestureRecognizer) {
        dismiss(animated: true, completion: nil)
    }
    
    
    func viewForZooming(in scrollView: UIScrollView) -> UIView? {
        return self.enlargeImage;
    }
    
    func scrollViewDidZoom(_ scrollView: UIScrollView) {
        let offsetX: CGFloat = (scrollView.bounds.size.width > scrollView.contentSize.width) ? (scrollView.bounds.size.width - scrollView.contentSize.width) * 0.5 : 0.0
        let offsetY: CGFloat = (scrollView.bounds.size.height > scrollView.contentSize.height) ? (scrollView.bounds.size.height - scrollView.contentSize.height) * 0.5 : 0.0
        enlargeImage.center = CGPoint(x: scrollView.contentSize.width * 0.5 + offsetX, y: scrollView.contentSize.height * 0.5 + offsetY)
    }
}
